package com.example.anliku.service.serviceImpl;

import com.example.anliku.entity.*;
import com.example.anliku.entity.clazzBody.clazzRetGxBody;
import com.example.anliku.mapper.ClazzMapper;
import com.example.anliku.mapper.KnowledgeMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.example.anliku.mapper.ExampleClazzMapper;
import com.example.anliku.service.ExampleClazzService;

import java.lang.System;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExampleClazzServiceImpl implements ExampleClazzService{

    @Resource
    private ExampleClazzMapper exampleClazzMapper;

    @Resource
    private ClazzMapper clazzMapper;
    @Override
    public Message deleteByPrimaryKey(Integer exampleClazzId) {
        return exampleClazzMapper.deleteByPrimaryKey(exampleClazzId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public int insert(ExampleClazz record) {
        return exampleClazzMapper.insert(record);
    }

    @Override
    public int insertSelective(ExampleClazz record) {
        return exampleClazzMapper.insertSelective(record);
    }

    @Override
    public ExampleClazz selectByPrimaryKey(Integer exampleClazzId) {
        return exampleClazzMapper.selectByPrimaryKey(exampleClazzId);
    }

    @Override
    public int updateByPrimaryKeySelective(ExampleClazz record) {
        return exampleClazzMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ExampleClazz record) {
        return exampleClazzMapper.updateByPrimaryKey(record);
    }

    @Override
    public Message insertList(List<ExampleClazz> list) {
        for (ExampleClazz exampleClazz : list) {
            Clazz clazz = new Clazz();
            clazz.setClazzName(exampleClazz.getExampleClazzClazzName());
            try {
                clazzMapper.insert(clazz);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return exampleClazzMapper.insertList(list) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertListGx(List<clazzRetGxBody> list) {
        //exampleClazzMapper
        List <ExampleClazz> exampleClazzList=new ArrayList();
        for(clazzRetGxBody clazzRetGxBody:list){
            List<ExampleClazz> aa= exampleClazzMapper.selectAllByExampleClazzClazzNameAndExampleClazzExampleId(clazzRetGxBody.getClazzName(),clazzRetGxBody.getExampleId());
            if(CollectionUtils.isEmpty(aa)){//没重复
                List<Clazz> clazzList=clazzMapper.selectAllByClazzName(clazzRetGxBody.getClazzName());//看课程名是否重复
                ExampleClazz exampleClazz=new ExampleClazz();
                if(CollectionUtils.isEmpty(clazzList)){//没重复
                    Clazz clazz = new Clazz();
                    clazz.setClazzName(clazzRetGxBody.getClazzName());
                    clazzMapper.insert(clazz);
                }
                exampleClazz.setExampleClazzClazzName(clazzRetGxBody.getClazzName());
                exampleClazz.setExampleClazzExampleId(clazzRetGxBody.getExampleId());
                exampleClazzList.add(exampleClazz);
            }else{//案例关系重复
                return Message.fail("课程与案例对应关系重复");
            }

        }
        return exampleClazzMapper.batchInsert(exampleClazzList) > 0 ? Message.success(null) : Message.fail(null);
    }
}
